package com.circle.rest.modular.nt.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.circle.rest.core.aop.Log;
import com.circle.rest.core.aop.Permission;
import com.circle.rest.core.common.enums.BusinessType;
import com.circle.rest.core.common.enums.ReadStatus;
import com.circle.rest.core.common.model.SuccessResponseData;
import com.circle.rest.core.common.page.PageInfoBT;
import com.circle.rest.core.exception.ServiceException;
import com.circle.rest.core.util.ToolUtil;
import com.circle.rest.modular.nt.model.Content;
import com.circle.rest.modular.nt.model.Inbox;
import com.circle.rest.modular.nt.service.IContentService;
import com.circle.rest.modular.nt.service.IInboxService;
import com.circle.rest.modular.nt.wrapper.InboxWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 通知收件箱 前端控制器
 * </p>
 *
 * @author 赵航
 * @since 2019-04-09
 */
@Api(value = "通知收件箱", tags = {"nt : 通知收件箱API"})
@RestController
@RequestMapping("/nt/inbox")
public class InboxController {

    @Autowired
    private IInboxService inboxService;

    @Autowired
    private IContentService contentService;

    /**
     * 查询通知收件箱列表
     */
    @ApiOperation("查询通知收件箱列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "index", value = "起始数据下标", dataType = "int"),
            @ApiImplicitParam(name = "size", value = "数据条数", dataType = "int"),
            @ApiImplicitParam(name = "userId", value = "用户编号", dataType = "String")
    })
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @Log(title = "查询通知收件箱列表", businessType = BusinessType.SELECT_LIST)
    @Permission
    public Object list(Integer index, Integer size, String userId) {
        if (ToolUtil.isEmpty(userId)) {
            throw new ServiceException("用户编号不能为空");
        }
        Page<Inbox> page = new Page<>(index == null ? 1 : index, size == null ? 15 : size);
        QueryWrapper<Inbox> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.orderByDesc("create_time");
        IPage<Map<String, Object>> mapIPage = inboxService.pageMaps(page, wrapper);
        PageInfoBT<Object> objectPageInfoBT = new PageInfoBT<>(new InboxWrapper(mapIPage).wrap());
        return new SuccessResponseData(objectPageInfoBT);
    }

    /**
     * 删除通知
     */
    @ApiOperation("删除通知收件箱")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "通知收件箱编号", required = true, dataType = "String")
    })
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @Log(title = "删除通知收件箱", businessType = BusinessType.DELETE)
    @Permission
    public Object delete(String id) {
        if (ToolUtil.isEmpty(id)) {
            throw new ServiceException("通知收件箱编号不能为空");
        }
        Inbox inboxOld = inboxService.getById(id);
        if (ToolUtil.isEmpty(inboxOld)) {
            throw new ServiceException("通知收件箱不存在，删除失败");
        }
        if (!inboxService.removeById(id)) {
            throw new ServiceException("删除失败");
        }
        return new SuccessResponseData();
    }


    /**
     * 查询通知信息
     */
    @ApiOperation("查询通知信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "通知收件箱编号", required = true, dataType = "String")
    })
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @Log(title = "查询通知信息", businessType = BusinessType.SELECT_ONE)
    @Permission
    public Object detail(String id) {
        if (ToolUtil.isEmpty(id)) {
            throw new ServiceException("通知收件箱编号不能为空");
        }
        Inbox inbox = inboxService.getById(id);
        if (ToolUtil.isEmpty(inbox)) {
            throw new ServiceException("通知收件箱不存在");
        }
        inboxService.read(inbox);
        Content content = contentService.getById(inbox.getContentId());
        return new SuccessResponseData(content);
    }

    /**
     * 用户通知标记已读
     */
    @ApiOperation("用户通知标记已读")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "通知收件箱编号(逗号分隔)", required = true, dataType = "String")
    })
    @RequestMapping(value = "/markRead", method = RequestMethod.GET)
    @Log(title = "用户通知标记已读", businessType = BusinessType.SELECT_ONE)
    @Permission
    public Object markRead(String ids) {
        if (ToolUtil.isEmpty(ids)) {
            throw new ServiceException("收件编号不能为空");
        }

        List<String> result = Arrays.asList(ids.split(","));
        UpdateWrapper<Inbox> wrapper = new UpdateWrapper<>();
        wrapper.in("id", result);
        Inbox inbox = new Inbox();
        inbox.setIsRead(ReadStatus.READ.getCode());
        inbox.setUpdateTime(LocalDateTime.now());
        inboxService.update(inbox, wrapper);
        return new SuccessResponseData();
    }

    /**
     * 用户通知标记已读（全部）
     */
    @ApiOperation("用户通知标记已读（全部）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户编号", required = true, dataType = "String")
    })
    @RequestMapping(value = "/markReadAll", method = RequestMethod.GET)
    @Log(title = "用户通知标记已读（全部）", businessType = BusinessType.SELECT_ONE)
    @Permission
    public Object markReadAll(String userId) {
        if (ToolUtil.isEmpty(userId)) {
            throw new ServiceException("用户编号不能为空");
        }
        UpdateWrapper<Inbox> wrapper = new UpdateWrapper<>();
        wrapper.in("user_id", userId);
        Inbox inbox = new Inbox();
        inbox.setIsRead(ReadStatus.READ.getCode());
        inbox.setUpdateTime(LocalDateTime.now());
        inboxService.update(inbox, wrapper);
        return new SuccessResponseData();
    }

}
